package com.easy.cms.material.service;



import com.easy.cms.material.model.Material;
import com.easy.cms.material.dao.MaterialRepository;
import com.easy.cms.material.model.MaterialQuery;
import com.easy.cms.base.CommonResult;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import jakarta.persistence.criteria.Predicate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
public class MaterialServiceImpl implements MaterialService {
    @Resource
    private MaterialRepository materialRepository;

    @Override
    @Transactional
    public CommonResult<Iterator<Material>> getMaterialList() {
        return CommonResult.success(materialRepository.findAll().iterator());
    }

    @Override
    @Transactional
    public CommonResult<List<Material>> searchMaterials(String code, String name) {
        return CommonResult.success(materialRepository.findByStandardCodeContainingOrNameContaining(code, name));
    }

    @Override
    @Transactional
    public CommonResult<Material> addMaterial(@Valid @NotNull(message = "物料信息不能为空") Material material) {
        // 若校验失败，Spring 会自动返回 400 错误
        
        return CommonResult.success(materialRepository.save(material));
    }

    @Override
    @Transactional
    public CommonResult<Material> updateMaterial(Material material) {
        return CommonResult.success(materialRepository.save(material));
    }

    @Override
    @Transactional
    public CommonResult<Page<Material>> getMaterialListPage(MaterialQuery materialQueryWithPagination) {
        Pageable pageable = PageRequest.of(materialQueryWithPagination.getPage(), materialQueryWithPagination.getSize());
        Specification<Material> spec = (root, query, cb) -> buildPredicates(root, cb, materialQueryWithPagination);
        return CommonResult.success(materialRepository.findAll(spec, pageable));
    }

    private Predicate buildPredicates(jakarta.persistence.criteria.Root<Material> root, jakarta.persistence.criteria.CriteriaBuilder cb, MaterialQuery materialQuery) {
        java.util.List<Predicate> predicates = new java.util.ArrayList<>();
        // 原有条件查询
        for (java.util.Map.Entry<String, String> entry : materialQuery.getConditions().entrySet()) {
            predicates.add(cb.like(root.get(entry.getKey()), "%" + entry.getValue() + "%"));
        }
        // 假设新增字段为 field1 和 field2，需根据实际情况修改
        if (materialQuery.getCodeLike() != null) {
            predicates.add(cb.like(root.get(Material.STANDARD_CODE), "%" + materialQuery.getCodeLike() + "%"));
        }
        if (materialQuery.getNameLike() != null) {
            predicates.add(cb.like(root.get(Material.NAME), "%" + materialQuery.getNameLike() + "%"));
        }
        return cb.and(predicates.toArray(new Predicate[0]));
    }

    @Override
    @Transactional
    public CommonResult<?> deleteMaterial(Long id) {
        materialRepository.deleteById(id);
        return CommonResult.success(null);
    }
}